﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace AIC_PN
{
    public partial class BaoCaoMau11_Update : System.Web.UI.Page
    {
        private static int ID;
        private string MyAction = MyConst.Action.Addnew;
        private string MyMaDM = "";

        #region Ham Su kien
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPermision())
            {
                formUpdate.Visible = false;
                if (!IsPostBack)
                {
                    BindDataToControls();
                }
            }
            else
            {
                Response.Redirect("index.php");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }


        protected void grdDanhMuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            ID = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == MyConst.Action.Update)
            {

                formUpdate.Visible = true;
                DataTable DanhMucTable = GetChiTietCanBo(ID);
                txtHoTen.Text = DanhMucTable.Rows[0][1].ToString();
                txtChucVu.Text = DanhMucTable.Rows[0][2].ToString();
                txtDonViID.Text = DanhMucTable.Rows[0][3].ToString();
                lblTenDonVi.InnerHtml = DanhMucTable.Rows[0][8].ToString();
                cboTapThe.SelectedValue = DanhMucTable.Rows[0][4].ToString();
                btnLuu.CommandName = MyConst.Action.Update;
            }
            else if (e.CommandName == MyConst.Action.Delete)
            {
                string sql = "DELETE FROM dbo.tblCanBo WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                BindDataToGrid("");
                MyCommon.AddMessage(this.Page.Master, "Xóa người dùng thành công", MyConst.MessageType.Success);
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = true;
            txtHoTen.Text = "";
            txtChucVu.Text = "";
            txtDonViID.Text = "";
            cboTapThe.SelectedIndex = 0;
            btnLuu.CommandName = MyConst.Action.Addnew;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(Session["userid"]);
            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                InsertDanhMuc(
                    txtHoTen.Text,
                    txtChucVu.Text,
                    Convert.ToInt32(txtDonViID.Text),
                    Convert.ToInt32(cboTapThe.SelectedValue)
                    );
                BindDataToGrid("");
                MyCommon.AddMessage(this.Page.Master, "Thêm người dùng thành công", MyConst.MessageType.Success);
            }
            else if (btnLuu.CommandName == MyConst.Action.Update)
            {
                UpdateDanhMuc(
                    txtHoTen.Text,
                    txtChucVu.Text,
                    Convert.ToInt32(txtDonViID.Text),
                    Convert.ToInt32(cboTapThe.SelectedValue),
                    ID
                    );
                BindDataToGrid("");
                MyCommon.AddMessage(this.Page.Master, "Cập nhật thông tin người dùng thành công", MyConst.MessageType.Success);
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = false;
        }
        #endregion Ham su kien

        #region Ham chuc nang

        protected void BindDataToGrid(string HoTen)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, HoTen));
            string sql = "SELECT CB.*,DV.TenDV FROM dbo.tblCanBo CB " +
                         "INNER JOIN dbo.tblDonVi DV ON DV.ID = CB.DonViID " +
                         "WHERE TenCanBo LIKE '%'+@HoTen+'%' OR @HoTen = ''";
            grdDanhMuc.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            grdDanhMuc.DataBind();
        }

        protected DataTable GetChiTietCanBo(int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            return SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT CB.*,DV.TenDV FROM dbo.tblCanBo CB INNER JOIN dbo.tblDonVi DV ON DV.ID = CB.DonViID WHERE CB.ID = @ID", arrParams);
        }

        protected void InsertDanhMuc(string HoTen, string ChucVu, int DonViID, int LaToChuc)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, HoTen));
            arrParams.Add(SqlHelper.CreateParameterObject("@ChucVu", SqlDbType.NVarChar, ParameterDirection.Input, ChucVu));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, DonViID));
            arrParams.Add(SqlHelper.CreateParameterObject("@LaToChuc", SqlDbType.Int, ParameterDirection.Input, LaToChuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
            string sql = "INSERT INTO dbo.tblCanBo(TenCanBo ,ChucVu ,DonViID ,LaToChuc ,CreateDate ,UpdateDate ,UserID)" +
                         "VALUES(@HoTen ,@ChucVu ,@DonViID ,@LaToChuc ,GETDATE() ,GETDATE() ,@UserID)";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void UpdateDanhMuc(string HoTen, string ChucVu, int DonViID, int LaToChuc, int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, HoTen));
            arrParams.Add(SqlHelper.CreateParameterObject("@ChucVu", SqlDbType.NVarChar, ParameterDirection.Input, ChucVu));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, DonViID));
            arrParams.Add(SqlHelper.CreateParameterObject("@LaToChuc", SqlDbType.Int, ParameterDirection.Input, LaToChuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            string sql = "UPDATE dbo.tblCanBo SET TenCanBo = @HoTen, ChucVu = @ChucVu, DonViID = @DonViID,LaToChuc = @LaToChuc, UpdateDate = GETDATE(), UserID = @UserID WHERE ID = @ID";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        public bool CheckPermision()
        {
            if (Session["username"] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion  Ham chuc nang

        protected void btnLuuBaoCao_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM dbo.tblBaoCaoMau11 WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count == 0)
            {
                btnLuu.CommandName = MyConst.Action.Addnew;
            }
            else
            {
                btnLuu.CommandName = MyConst.Action.Update;
            }

            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                string sSQL = "INSERT INTO dbo.tblBaoCaoMau11( KyBaoCaoID_DM ,DonViID ,CM_SoBaoCao ,CM_ThoiGianBaoCao ,IsCommon ,CreateDate ,UpdateDate ,UserID)" +
                              "VALUES ( @KyBaoCaoID_DM,@DonViID,@CM_SoBaoCao,@CM_ThoiGianBaoCao,1,GETDATE(),GETDATE(),@UserID)";

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtSoBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThoiGianBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtNgayBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"])));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                MyCommon.AddMessage(this.Master, "Lưu thông tin báo cáo thành công", MyConst.MessageType.Success);
            }
            else
            {
                string sSQL = "UPDATE dbo.tblBaoCaoMau11 SET CM_SoBaoCao = @CM_SoBaoCao, CM_ThoiGianBaoCao = @CM_ThoiGianBaoCao " +
                                "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtSoBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThoiGianBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtNgayBaoCao.Text.Trim()));


                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

                MyCommon.AddMessage(this.Master, "Lưu thông tin báo cáo thành công", MyConst.MessageType.Success);
            }
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = true;
                grdDanhMuc.Visible = true;
                grdDanhMuc.EditIndex = -1;
                BindDataToGrid("");
            }
            else
            {
                btnLuu.Visible = false;
                grdDanhMuc.Visible = false;
            }
        }

        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {

                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];

                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);


            }


        }
    }
}
